﻿Public Class ViewExplorer
    Inherits ExplorerTreeList

    Private ViewPL As PLBase

    Public Sub New(ByVal aQueryName As String, ByVal aWhere As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        Dim aParamFrm As MqParamForm
        Dim aExecQuery As DialogResult = DialogResult.OK
        Dim aDataset As DataSet
        Dim aMetaDt As MetaDataTable = New MetaDataTable()
        '

        Me.Cursor = Cursors.WaitCursor

        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '
        Dim aguid As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        Dim frmCaption As String = Nothing

        Using aMq As MetaQuery = MetaQuery.GetMetaQueryById(aguid)
            Dim reader As SqlClient.SqlDataReader = Nothing
            '
            '
            If aMq.CommandTypeID = IdRepository.METALIST_METACOMMANDTYPE_SP AndAlso aMq.MetaParams.Rows.Count > 0 Then

                aParamFrm = New MqParamForm(aMq.QueryName)
                aExecQuery = aParamFrm.ShowDialog()
                'On dit OK pour la form de Parametre
                If aExecQuery = DialogResult.OK Then
                    'On va chercher les Parametre de la SP
                    'sqlcmd = MetaQuery.GetSPCmdByMeta(aMq, False)
                    sqlcmd = aMq.InitQueryParams
                    'On map les parametres de la SP avec les valeurs de la form aParamFrm.
                    'For Each param As SqlParameter In sqlcmd.Parameters
                    '    If param.Direction = ParameterDirection.Input Then
                    '        If Not IsDBNull(aParamFrm.SelectedParam(param.ParameterName)) Then
                    '            param.Value = aParamFrm.SelectedParam(param.ParameterName)
                    '        End If
                    '    End If
                    'Next

                    'On prend les parametre défini par la MQ, pour les retrouver dans le sqlCmd.Parameters
                    'Les metaColumns on la priorité
                    Dim param As SqlClient.SqlParameter
                    For Each row As DataRow In aMq.MetaParams.Rows
                        If sqlcmd.Parameters.Contains(row("ColumnName").ToString) Then
                            param = sqlcmd.Parameters(row("ColumnName").ToString)
                            If param IsNot Nothing AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                                param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                            End If
                        Else
                            MessageWindow.ShowMessage(New Exception(String.Format("View/Audit Execution, SQL Parameter not found: {0}", row("ColumnName"))))
                        End If
                    Next
                End If
                If Not aExecQuery = DialogResult.Cancel Then
                    'On Passe la sqcmd qui a les paramêtre de rentré
                    'aMq.FillResultQuery(sqlCmd:=sqlcmd)
                    aMetaDt.QueryName = aMq.QueryName
                    aMetaDt.GetMetaCommand()
                    aMetaDt.Command = sqlcmd
                    aMetaDt.FillBusinessApplicationData()
                End If
            Else
                aMetaDt.QueryName = aMq.QueryName
                aMetaDt.GetMetaCommand()
                aMetaDt.FillBusinessApplicationData()
            End If
            '
            If aMetaDt.Rows.Count > 0 Then
                aDataset = New DataSet("AuditView")
                aDataset.Tables.Add(aMetaDt)

                aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = Nothing, .Name = "AuditView", .FormCtrlCD = Nothing}
                aGridControl.AllowCreate = False
                aGridControl.AllowDelete = False
                aGridControl.AllowUpdate = False

                aGridControl.Dataset = aDataset
                aGridControl.SecureGrid = False

                aGridControl.GridActionOptionList.Add(New ActionOption("Export", "Export", "Export to CSV", AddressOf ExportToCSV, True, True, ))
                '
                'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
                'Comme ca on change le Detail selon la ligne dans L'explorer
                For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
                    If aFrm.Name = PanelName Then
                        aFrm.Controls.Clear()
                        aFrm.Text = aMq.Caption
                        aFrm.Controls.Add(aGridControl)
                        aFrm.Show()
                        aFrm.Focus()
                    End If
                    aFrm.Refresh()
                Next
            Else
                MessageWindow.ShowMessage("No Data Return")
            End If
        End Using
        '
        Me.Cursor = Cursors.Default
    End Sub

End Class
